## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
import sys
import subprocess
import Options
from waflib.Errors import WafError

def configure(conf):
    if Options.options.enable_mpi:
        # try to detect openmpi installation
        mpi = conf.check_cfg(path='mpic++', args='-showme',
                             package='', uselib_store='MPI', mandatory=False)
        if mpi:
            conf.env.append_value('DEFINES_MPI', 'NS3_OPENMPI')
        else:
            # try the MPICH2 flags
            mpi = conf.check_cfg(path='mpic++', args='-compile-info -link-info',
                                 package='', uselib_store='MPI', mandatory=False)
            if mpi:
                conf.env.append_value('DEFINES_MPI', 'NS3_MPICH')
        if mpi:
            conf.env.append_value('DEFINES_MPI', 'NS3_MPI')
            conf.env['ENABLE_MPI'] = True
            for libpath in conf.env.LIBPATH_MPI:
                if 'mpi' in libpath:
                    conf.env.append_value('LINKFLAGS_MPI', '-Wl,-rpath='+libpath)
            conf.report_optional_feature("mpi", "MPI Support", True, '')            
        else:
            conf.report_optional_feature("mpi", "MPI Support", False, 'mpic++ not found')
    else:
        conf.report_optional_feature("mpi", "MPI Support", False, 'option --enable-mpi not selected')


def build(bld):
    env = bld.env
    sim = bld.create_ns3_module('mpi', ['core', 'network'])
    sim.source = [
        'model/distributed-simulator-impl.cc',
        'model/mpi-interface.cc',
        'model/mpi-receiver.cc',
        ]

    headers = bld.new_task_gen(features=['ns3header'])
    headers.module = 'mpi'
    headers.source = [
        'model/distributed-simulator-impl.h',
        'model/mpi-interface.h',
        'model/mpi-receiver.h',
        ]

    if env['ENABLE_MPI']:
        sim.use.append('MPI')

    if bld.env['ENABLE_EXAMPLES']:
        bld.add_subdirs('examples')
      
    bld.ns3_python_bindings()
